import pandas as pd
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline


def preprocess_data(data_path):
    """
    对给定路径下的数据进行预处理。

    参数:
    data_path (str): 数据集的路径。

    返回:
    X (DataFrame): 处理后的特征数据。
    y (Series): 目标变量数据。
    preprocessor (Pipeline): 用于预处理的管道对象，可在测试集上复用。
    """
    # 加载数据集
    data = pd.read_csv(data_path)

    # 提取特征和目标变量
    X = data.drop('Attrition', axis=1)
    y = data['Attrition']

    # 对数值型和分类型特征分别进行处理
    numeric_features = X.select_dtypes(include=['int64']).columns
    categorical_features = X.select_dtypes(include=['object']).columns

    numeric_transformer = Pipeline(steps=[
        ('scaler', StandardScaler())])

    categorical_transformer = Pipeline(steps=[
        ('onehot', OneHotEncoder(handle_unknown='ignore'))])

    preprocessor = ColumnTransformer(
        transformers=[
            ('num', numeric_transformer, numeric_features),
            ('cat', categorical_transformer, categorical_features)])

    # 对特征数据进行预处理
    X = preprocessor.fit_transform(X)

    return X, y, preprocessor


preprocess_data('../data/train.csv')
